package com.xizi.yygh.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

//处理跨域
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        //创建跨域配置对象
        CorsConfiguration config = new CorsConfiguration();
        //设置方法AllowedMethod 源AllowedOrigin 头AllowedHeader
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        //创建URL跨域配置资源对象
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        //资源对象注册配置所有的路径
        source.registerCorsConfiguration("/**", config);
        //返回一个跨域web过滤器
        return new CorsWebFilter(source);
    }
}
